<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>流程实例列表</title>
	<#include "/common/resource.ftl">
</head>
<body>

<div class="ui-operation">
    <div class="ui-search-items layui-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="status" class="layui-input">
                        <option value="">请选择</option>
                        <option value="1">审批中</option>
                        <option value="2">已通过</option>
                        <option value="3">已驳回</option>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="ui-buttons">
        <div class="layui-btn-group tools">
            <@auth code='workflowInstance_add'><button class="layui-btn layui-btn-normal open-dialog" p="url:'${params.contextPath}/view/base/workflowInstance/workflowInstance_edit',title:'添加流程实例',width:'80%',height:'100%'"><i class="fa fa-plus"></i>添加</button></@auth>
            <@auth code='workflowInstance_update'><button class="layui-btn layui-btn-normal modify-button"><i class="fa fa-pencil"></i>修改</button></@auth>
            <@auth code='workflowInstance_delete'><button class="layui-btn layui-btn-normal remove-button" reurl="${params.contextPath}/web/workflowInstance/delete"><i class="fa fa-remove"></i>删除</button></@auth>
            <button class="layui-btn layui-btn-normal submit-button" p="url:'${params.contextPath}/view/base/workflowTask/workflowTask_approval?sbm=1',title:'提交审批',width:'600px',height:'450px'"><i class="fa fa-gavel"></i>提交审批</button>
            <button class="layui-btn layui-btn-normal print-button"><i class="fa fa-print"></i>打印</button>
        </div>
        <div class="ui-searchs">
            <input type="button" value="搜索" class="layui-btn layui-btn-danger search-button"/>
        </div>
    </div>
</div>
<div class="ui-content" style="padding-left: 10px;padding-right:10px;">
    <table id="datagrid" options="url:'${params.contextPath}/web/workflowInstance/list',params:'getSearchParams',dblClickRow:'showDetail'">
        <thead>
			<tr>
				<th data-options="field:'id',checkbox:true"></th>
				<th data-options="field:'title',width:300,align:'left'">标题</th>
				<th data-options="field:'statusStr',width:100,align:'left',formatter:formatState">审批状态</th>
				<th data-options="field:'createTimeStr',width:200,align:'left'">创建时间</th>
			</tr>
        </thead>
    </table>
</div>
<script type="text/javascript">
    var currentSelectRow = "";
    function getSearchParams() {
        return {
            title: $("input[name='title']").val(),
            status: $("select[name='status']").val()
        };
    }

    var showDetail = function (index, row) {
        <@auth code='workflowInstance_detail'>
            var url = "${params.contextPath}" + row.defineFormPath + "_detail?his=1&id=" + row.formId + "&formId=" + row.formId;
            DialogManager.openDialog("url:'" + url + "',title:'流程实例详情',width:'80%',height:'100%'");
        </@auth>
    };

    //提交审批
    var submitApproval = function (type) {
        DialogManager.closeAll();
        if (!currentSelectRow || !type) {
            $.message("参数错误");
            return;
        }
        var p = $("." + type + "-button").attr("p");
        var params = eval("({" + (p || "") + "})");
        params.url = $.getUrl(params.url, "workflowDefineId", currentSelectRow.workflowDefineId);
        params.url = $.getUrl(params.url, "instanceId", currentSelectRow.id);
        params.url = $.getUrl(params.url, "stepNum", 0);
        DialogManager.openDialog(JSON.stringify(params).replace("{", "").replace("}", ""));
    };

    //formatter:formatState
    function formatState(row) {
        if (row.status == 2) {
            return '<span class="ui-accept">' + row.statusStr + '</span>';
        } else if (row.status == 3) {
            return '<span class="ui-stop">' + row.statusStr + '</span>';
        }
        return row.statusStr;
    }

    $(function () {
        $(".remove-button").click(function () {
            var row = DataGrid.getSelectRow();
            if (!row) {
                $.message("请选中删除记录")
                return false;
            }
            if (row.status == 1) {
                $.message("审批中流程申请单无法删除")
                return false;
            }
            var url = $(this).attr("reurl");
            layer.confirm('确定删除该选中记录', function () {
                $.ajaxRequest({
                    type: 'post',
                    url: url,
                    data: {ids: row.id},
                    success: function (data) {
                        $.message(data.message);
                        if (data.success) {
                            DataGrid.reload();
                        }
                    }
                });
            });
        });

        $(".submit-button").click(function () {
            var row = DataGrid.getSelectRow();
            if (!row) {
                $.message("请选择一行记录")
                return false;
            }
            if (row.status != 3) {
                $.message("非[已驳回]状态的申请单，无法提交审批")
                return false;
            }
            currentSelectRow = row;
            submitApproval("submit");
        });

        $(".modify-button").click(function () {
            var row = DataGrid.getSelectRow();
            if (!row) {
                $.message("请选中一条记录")
                return false;
            }
            if (row.status != 3) {
                $.message("非[已驳回]状态的申请单，无法修改")
                return false;
            }
            var p = "url:'${params.contextPath}" + row.defineFormPath + "_edit?id=" + row.formId + "',title:'编辑流程实例',width:'80%',height:'100%'";
            DialogManager.openDialog(p);
        });

        $(".print-button").click(function () {
            var row = DataGrid.getSelectRow();
            if (!row) {
                $.message("请选中一条记录")
                return false;
            }
            var url = "${params.contextPath}" + row.defineFormPath + "_print?id=" + row.formId;
            DialogManager.openDialog("url:'" + url + "',title:'流程实例详情',width:'80%',height:'100%'");
        });
    });
</script>
</body>

</html>
